adminPanel.controller("UserExtraAcademicController",['cacheFactory','$http','$scope','$rootScope','$q','$filter',function(cacheFactory,$http,$scope,$rootScope,$q,$filter){
	$rootScope.studentProfile=null;	
	$rootScope.showGrid=false;
	$rootScope.showStrength=false;
	$rootScope.overallGrid={};
	$rootScope.searchCriteria.recordType="EXTRA_ACADEMICS"; //this is important
	
	$rootScope.selectedStudent="";
	var  thisObject=this;
	$rootScope.studentProfiles=null;
	
	$rootScope.isRecommendationFound=false;
	//paging
	$rootScope.gridPageNumber=0;
	$rootScope.recommendationPageNumber=0;
	$rootScope.strengthPageNumber=0;
	$rootScope.graphPageNumber=0;
	
	$rootScope.gridData=[];
	$rootScope.recommendations={};
	$rootScope.strengthData=[];
	
	$rootScope.getCertificateName=function(certId){
		var certificates = cacheFactory.getCertificationList();
		var filtered=$filter('filter')(certificates,{id:certId});
		if(filtered && filtered.length>0){
			return filtered[0].certificationName;
		}	
	}
	
	$rootScope.next=function(name){
		if(name==='GRID'){
			$rootScope.gridPageNumber--;
			thisObject.getGridData();
		}
		if(name==='STRENGTH'){
			$rootScope.strengthPageNumber--;
			//thisObject.getDetails();
		}
		if(name==='RECOMMENDATION'){
			$rootScope.recommendationPageNumber--;
			thisObject.getRecommendations();
		}
	}
	
	$rootScope.previous=function(name){
		if(name==='GRID'){
			$rootScope.gridPageNumber++;
			thisObject.getGridData();
		}
		if(name==='STRENGTH'){
			$rootScope.strengthPageNumber++;
			thisObject.getStrengthData();
		}
		if(name==='RECOMMENDATION'){
			$rootScope.recommendationPageNumber++;
			thisObject.getRecommendations();
		}
	}
	
	//reset all paging counters
	this.resetPaging=function(){
		$rootScope.gridPageNumber=0;
		$rootScope.recommendationPageNumber=0;
		$rootScope.strengthPageNumber=0;
		$rootScope.graphPageNumber=0;
	}
	
	//render all four quads on the page
	this.renderInformation=function(data){
		if(data && data.e=='STUDENT_PROFILE_CHANGED' && $rootScope.contentUrl.indexOf("user_extra_academics")>-1){
			$rootScope.studentProfile=data.d;
			thisObject.resetPaging();
			thisObject.getRecommendations();
			thisObject.getGridData();
			thisObject.getStrengthData();
		}
	};
	
	//attach handler to main screen select button
	RegisterEventHandler("StudentExtraAcademics",thisObject.renderInformation);
	
	$rootScope.getTotalCount=function(quad){
		if(quad=='GRID')
			return Math.floor($rootScope.studentProfile.totalGradeInfo.length/3)+(($rootScope.studentProfile.totalGradeInfo.length%3==0)?0:1);
		if(quad=='STRENGTH')
			return Math.floor($rootScope.studentProfile.totalGradeInfo.length/3)+(($rootScope.studentProfile.totalGradeInfo.length%3==0)?0:1);
		if(quad=='RECOMMENDATION')
			return Math.floor($rootScope.studentProfile.totalGradeInfo.length);
	}
	
	this.getStrengthData=function(){
		$rootScope.strengthData=[];
		$rootScope.searchCriteria.pageNumber=$rootScope.strengthPageNumber;
		$http.post(BASE_URL+"user/handleRequest/USER_EXTRA_ACADEMICS_DASHBOARD_OVERALL_STRENGTH",$rootScope.searchCriteria).success(function(data){
			if(data.status=='SUCCESS'){
				if(data.result && data.result.length>0){
					$rootScope.strengthData=data.result[0];
				}
				else{
				}
			}else if(data.status=='NOT_FOUND'){
			}
			else{
				alert(data.status +":"+data.msg);
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
	}
	
	this.getGridData=function(){
		$rootScope.gridData=[];
		$rootScope.searchCriteria.pageNumber=$rootScope.gridPageNumber;
		$http.post(BASE_URL+"user/handleRequest/USER_EXTRA_ACADEMICS_DASHBOARD_OVERALL_GRID",$rootScope.searchCriteria).success(function(data){
			if(data.status=='SUCCESS'){
				if(data.result && data.result.length>0){
					$rootScope.gridData=data.result[0];
					$rootScope.showGrid=true;
				}
				else{
					$rootScope.showGrid=false;
				}
			}else if(data.status=='NOT_FOUND'){
				$rootScope.showGrid=false;
			}
			else{
				$rootScope.showGrid=false;
				alert(data.status +":"+data.msg);
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
	}
	this.getRecommendations= function(){
		$rootScope.recommendations=[];
		$rootScope.searchCriteria.gradeId=$rootScope.studentProfile.totalGradeInfo[$rootScope.recommendationPageNumber].key;
		$http.post(BASE_URL+"user/handleRequest/USER_EXTRA_ACADEMICS_DASHBOARD_OVERALL_RECOMMENDATION",$rootScope.searchCriteria).success(function(data){
			if(data.status=='SUCCESS'){
				if(data.result && data.result.length>0){
					$rootScope.recommendations=data.result;
					$rootScope.isRecommendationFound=true;
				}
				else{
					$rootScope.isRecommendationFound=false;
				}
			}else if(data.status=='NOT_FOUND'){
				$rootScope.isRecommendationFound=false;
			}
			else{
				$rootScope.isRecommendationFound=false;
				alert(data.status +":"+data.msg);
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
	};
	
	this.init=function(selectedStudentProfile,userDashboard){
		userDashboard.selectStudent(selectedStudentProfile.entityId);
		$rootScope.studentProfile=selectedStudentProfile;
	};
	$rootScope.notNull = function(p){
	    if (p != null){
	        return p;
	    } else{
	        return;
	    }
	};
}]);